package com.sirenbang.easyshop.service.impl;

import java.util.List;

import com.sirenbang.easyshop.dao.UserDao;
import com.sirenbang.easyshop.dao.impl.UserDaoJDBCImpl;
import com.sirenbang.easyshop.domain.User;
import com.sirenbang.easyshop.exception.ClientException;
import com.sirenbang.easyshop.service.UserService;
import com.sirenbang.easyshop.utils.DBUtils;
import com.sirenbang.easyshop.utils.Page;
import com.sirenbang.easyshop.utils.TransactionManager;
import com.sirenbang.easyshop.utils.UserSearchHelper;


public class UserServiceImpl implements UserService {

	public void add(User user) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		UserDao dao=new UserDaoJDBCImpl();
		
		manager.beginTransaction();	
		try{	
			dao.add(user);
			manager.commitAndClose();
		}catch(ClientException e){
			manager.rollbackAndClose();
		}
		

	}

	public void delete(String userName) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		UserDao dao=new UserDaoJDBCImpl();
		
		manager.beginTransaction();	
		try{	
			dao.deleteByName(userName);
			manager.commitAndClose();
		}catch(ClientException e){
			manager.rollbackAndClose();
		}

	}

	public List<User> loadAll() {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		UserDao dao=new UserDaoJDBCImpl();
		List<User> userList=null;
		
		manager.beginTransaction();	
		try{	
			userList=dao.loadAll();
			manager.commitAndClose();
		}catch(ClientException e){
			manager.rollbackAndClose();
		}
		
		return userList;
	}

	public User search(String userName) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		UserDao dao=new UserDaoJDBCImpl();
		User user=null;
		
		manager.beginTransaction();	
		try{	
			user=dao.getByName(userName);
			manager.commitAndClose();
		}catch(ClientException e){
			manager.rollbackAndClose();
		}
		
		return user;
		
	}

	public void update(User user) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		UserDao dao=new UserDaoJDBCImpl();
		
		manager.beginTransaction();	
		try{	
			dao.update(user);
			manager.commitAndClose();
		}catch(ClientException e){
			manager.rollbackAndClose();
		}
		
	}

	public void setUserStatus(String userName, Integer status) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		UserDao dao=new UserDaoJDBCImpl();
		
		manager.beginTransaction();	
		try{	
			dao.setStatus(userName, status);
			manager.commitAndClose();
		}catch(ClientException e){
			manager.rollbackAndClose();
		}
		
	}

	public Page loadPageUser(Page page, UserSearchHelper userSearchHelper) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		UserDao dao=new UserDaoJDBCImpl();
		List<User> userList=null;
		
		manager.beginTransaction();	
		try{	
			userList=dao.loadScopeUser(userSearchHelper, page.getStartIndex(), page.getEndIndex());
			page.setPageContent(userList);
			manager.commitAndClose();
		}catch(ClientException e){
			manager.rollbackAndClose();
		}
		
		return page;
	}

	public Integer countUserByHelper(UserSearchHelper userSearchHelper) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		UserDao dao=new UserDaoJDBCImpl();
		Integer cnt=null;
		
		manager.beginTransaction();	
		try{	
			cnt=dao.countUserByHelper(userSearchHelper);
			manager.commitAndClose();
		}catch(ClientException e){
			manager.rollbackAndClose();
		}
		
		return cnt;
	}

}
